package com.treasure.mall.biz.entity.marketing;

import com.treasure.mall.biz.entity.BasePO;
import com.treasure.mall.biz.entity.user.MerchantPO;
import com.treasure.mall.zbq.common.Jsr310Utils;
import com.treasure.mall.zbq.common.ienum.marketing.RedPacketEnums;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 红包
 *
 * @author allan
 * @version 1.0.0
 * @date 2020/8/1
 */
@Entity
@Table(name = "zb_red_packet")
@Setter
@Getter
@Cacheable(false)
public class RedPacketPO extends BasePO {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;
    @Column(name = "title")
    private String title;
    @ManyToOne
    @JoinColumn(name = "merchant_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private MerchantPO merchantPO;
    /**
     * 红包金额
     */
    @Column(name = "amount", scale = 2, precision = 9)
    private BigDecimal amount = BigDecimal.ZERO;
    /**
     * 已抢的金额
     */
    @Column(name = "grab_amount", scale = 2, precision = 9)
    private BigDecimal grabAmount = BigDecimal.ZERO;
    /**
     * 红包背景图
     */
    @Column(name = "back_pic")
    private String backPic;
    /**
     * 红包个数
     */
    @Column(name = "num")
    private int num = 1;
    @Column(name = "grab_num")
    private int grabNum;
    @Column(name = "red_packet_status")
    private RedPacketEnums.Status status;
    @Column(name = "red_packet_type")
    private RedPacketEnums.Type type;
    @Column(name = "start_time")
    private LocalDateTime startTime;
    @Column(name = "is_enabled")
    private boolean enabled;

    public int remainNum() {
        return this.num - this.grabNum;
    }

    public BigDecimal remainAmount() {
        return this.amount.subtract(this.grabAmount);
    }

    public String startTimeStr() {
        return Jsr310Utils.DateTime.asString(this.startTime);
    }
}
